package com.jianneng.jdg.model.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.com.jianneng.jdg.R;

public class FragmentSchool extends Fragment implements OnClickListener {
	private RelativeLayout mCourse, mEduction, mApply;
	private Fragment mOnlineCourse, mTrianEduction, mOnlineApply;
	private Fragment currentFragment;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater
				.inflate(R.layout.fragment_school, container, false);
		mCourse = (RelativeLayout) view.findViewById(R.id.rl_circle_square);
		mEduction = (RelativeLayout) view.findViewById(R.id.rl_circle_workfriend);
		mApply = (RelativeLayout) view.findViewById(R.id.rl_circle_ask);
		mCourse.setOnClickListener(this);
		mEduction.setOnClickListener(this);
		mApply.setOnClickListener(this);
		return view;

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		super.onActivityCreated(savedInstanceState);
		initChildFragment();
	}

	private void initChildFragment() {
		if (mOnlineCourse == null) {
			mOnlineCourse = new FragmentCourse();
		}
		if (!mOnlineCourse.isAdded()) {

			getChildFragmentManager().beginTransaction()
					.add(R.id.rl_fragment, mOnlineCourse).commit();
			currentFragment = mOnlineCourse;

		} else {
			getChildFragmentManager().beginTransaction().show(mOnlineCourse);
		}
	}

	// 切换fragment
	private void showChildCircleFragment(FragmentTransaction transaction,
			Fragment fragment) {
		if (currentFragment == fragment){
			return;
		}
		if (!fragment.isAdded()) {
			transaction.hide(currentFragment).add(R.id.rl_fragment, fragment)
					.commit();
		} else {
			transaction.hide(currentFragment).show(fragment).commit();
		}
		currentFragment = fragment;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_circle_square:
			if (mOnlineCourse == null) {
				mOnlineCourse = new FragmentCourse();
			}
			showChildCircleFragment(getChildFragmentManager()
					.beginTransaction(), mOnlineCourse);

			break;
		case R.id.rl_circle_workfriend:
			if (mTrianEduction == null) {
				mTrianEduction = new FragmentCircleWorkFriend();
			}
			showChildCircleFragment(getChildFragmentManager()
					.beginTransaction(), mTrianEduction);
			
			break;

		case R.id.rl_circle_ask:
			if (mOnlineApply == null) {
				mOnlineApply = new FragmentApply();
			}
			showChildCircleFragment(getChildFragmentManager()
					.beginTransaction(), mOnlineApply);
			break;
		default:
			break;
		}

	}

}
